package com.tanhua.dubbo.api;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Long friendId) {
        //1、保存自己的好友数据
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        //1.1 判断好友关系是否存在
        Friend one = mongoTemplate.findOne(query1, Friend.class);
        if(!mongoTemplate.exists(query1,Friend.class)) {
            //1.2 如果不存在，保存
            Friend friend1 = new Friend();
            friend1.setUserId(userId);
            friend1.setFriendId(friendId);
            friend1.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend1);
        }
        //2、保存好友的数据
        Query query2 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        Friend one1 = mongoTemplate.findOne(query2, Friend.class);
        //2.1 判断好友关系是否存在
        if(!mongoTemplate.exists(query2,Friend.class)) {
            //2.2 如果不存在，保存
            Friend friend1 = new Friend();
            friend1.setUserId(friendId);
            friend1.setFriendId(userId);
            friend1.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend1);
        }
    }

    @Override
    public List<Friend> findByUserId(Long userId, Integer page, Integer pagesize, List<Long> list) {
        Criteria criteria = new Criteria();
        if (list.size()==0){
            criteria = Criteria.where("userId").is(userId);
        }else {
            criteria = Criteria.where("userId").is(userId).and("friendId").in(list);
        }
        Query query = new Query(criteria);
        long count = mongoTemplate.count(query,Friend.class);
        if (count<(page-1)*pagesize) {
            if (count==0){
                return new ArrayList<Friend>();
            }else {
                page = Integer.valueOf(String.valueOf(count))/pagesize;
            }
        }
         query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Friend.class);
    }

    @Override
    public Long getCount(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);

        return mongoTemplate.count(new Query(criteria),Friend.class);
    }


    @Override
    public Boolean delectByid(Long userId, Long id) {
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("friendId").is(id));
        Query query2 = Query.query(Criteria.where("userId").is(id).and("friendId").is(userId));
        //2.1 判断好友关系是否存在
        if(mongoTemplate.exists(query1, Friend.class)) {
            mongoTemplate.remove(query1,Friend.class);
            mongoTemplate.remove(query2,Friend.class);
            return true;
        }
        return false;
    }

    @Override
    public Map<Long, Friend> getBymyid(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = new Query(criteria);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        Map<Long, Friend> map = new HashMap<>();
        for (Friend friend : friends) {
            map.put(friend.getFriendId(),friend);
        }
        return map;
    }
}
